package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.HealthUpLoadDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryData;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Examination;
import com.itheima.pojo.ReportList;
import com.itheima.service.HealthUpLoadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service(interfaceClass = HealthUpLoadService.class)
@Transactional
public class HealthUpLoadServiceImpl implements HealthUpLoadService {

    @Autowired
    private HealthUpLoadDao healthUpLoadDao;

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        QueryData queryData = queryPageBean.getQueryData();
        PageHelper.startPage(currentPage, pageSize);
        Page<ReportList> page = healthUpLoadDao.selectByCondition(queryData);
        PageResult pr = new PageResult(page.getTotal(), page.getResult());
        return pr;
    }

    @Override
    public void add(ReportList reportList) {
        healthUpLoadDao.add(reportList);
    }

    @Override
    public ReportList findByPhone(String phone) {
        ReportList report = healthUpLoadDao.findByPhone(phone);
        return report;
    }

    @Override
    public int findByPhoneFromReportlist(String phoneNumber) {
        int count = healthUpLoadDao.findByPhoneFromReportlist(phoneNumber);
        return count;
    }

    @Override
    public boolean deletes(Integer[] id) {
        StringBuffer buf = new StringBuffer();
        for (Integer i : id) {
            buf.append(i + ",");
        }
        if (buf.length() > 0) {
            String str = buf.substring(0, buf.length() - 1);
            int count = healthUpLoadDao.findStatus(str);
            if(count>0){
                return false;
            }
            healthUpLoadDao.deletes(str);
            return true;
        }
        return false;
    }

    @Override
    public void addExcel(List<Examination> list) {
        if(list!=null && list.size()>0){
            for (Examination examination : list) {
                healthUpLoadDao.addExcel(examination);
            }
        }
    }

}
